
var util = require('../../utils/util.js');

const app = getApp();
var _openid = app.getOpenId();

Page({
  data: {
    carNumber: 0,    //车牌号
    nickName: "",    //车主的昵称
    telNum: "",      //车主电话号码
    areaName: "",    //小区大厦名称
    parkingNumber: "", //车位编号
    longitude: "",  //发布车位的精度
    latitude: "",  //发布车位的纬度
    rentPrice: "", //出租价格
    index: 0,
    startDate: '2018-05-07',
    endDate: '2022-09-05-07',
    startTime: '00:00',
    endTime: '00:00',
    },

  onLoad() {
  },

  onShow() {
    var that = this;
    that.checkUserStatus();
  },
  checkUserStatus: function () {
    var that = this;
    var wxPubApplyStatus = wx.getStorageSync("wxPubApplyStatus");

    if (wxPubApplyStatus != 2) {
      wx.showModal({
        title: '提示',
        content: "您的车位未通过审核或未申请",
        success: function (res) {

          if (res.confirm) {
            console.log('用户点击确定');
            wx.navigateTo({
              url: "/pages/bindLicensePlate/index"
            })
          } else if (res.cancel) {

          }
        }
      })
      return false;
    }
    return true;
  },

  nickNameInput: function (e) {
    console.log('业主昵称' + e.detail.value);
    this.setData({
      nickName: e.detail.value
    })
  },

  telInput: function (e) {
    console.log('业主电话' + e.detail.value);
    this.setData({
      telNum: e.detail.value
    })
  },

  areaNameInput: function (e) {
    console.log('小区/大厦' + e.detail.value);
    this.setData({
      areaName: e.detail.value
    })
  },

  parkingNumInput: function (e) {
    console.log('车位号' + e.detail.value);
    this.setData({
      parkingNumber: e.detail.value
    })
  },


  createTimeInput: function (e) {
    console.log('起租时间:' + e.detail.value);
    this.setData({
      startTime: e.detail.value
    })
  },

  endTimeInput: function (e) {
    console.log('停租时间:' + e.detail.value);
    this.setData({
      endTime: e.detail.value
    })
  },


  rentPriceInput: function (e) {
    console.log('出租价格:' + e.detail.value);
    this.setData({
      rentPrice: e.detail.value
    })
  },


  rentRemarkInput: function (e) {
    console.log(e.detail.value);
    this.setData({
      rentRemark: e.detail.value
    })
  },

  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  bindStartDateChange: function (e) {
    var that = this;
    this.setData({
      startDate: e.detail.value
    });
  },



  bindEndDateChange: function (e) {
    this.setData({
      endDate: e.detail.value
    });
  },

  bindStartTimeChange: function (e) {
    this.setData({
      startTime: e.detail.value
    })
  },

  bindEndTimeChange: function (e) {
    this.setData({
      endTime: e.detail.value
    })
  },



  releaseParking: function () {
    var that = this;
    console.log("发布车位时需要提交的参数:" +
      this.data.areaName,
      this.data.parkingNumber,
      start,
      end,
      this.data.rentPrice,
      this.data.longitude,
      this.data.latitude,
      this.data.rentRemark
    );
    if (!that.checkUserStatus()) {
      return;
    }
    var longitude = wx.getStorageSync('wxLongitude');
    var latitude = wx.getStorageSync('wxLatitude');
    this.setData({
      latitude: latitude,
      longitude: longitude
    });

    var start = this.data.startDate + ' ' + this.data.startTime;
    var end = this.data.endDate + ' ' + this.data.endTime;

    wx.request({
      url: app.globalData.domain + app.globalData.subDomain + '/parking/pubParking?',
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        'openid': _openid,
        'parking.areaName': this.data.areaName,
        'parking.parkingNumber': this.data.parkingNumber,
        'parking.startTime': start,
        'parking.endTime': end,
        'parking.rentPrice': this.data.rentPrice,
        'parking.longitude': this.data.longitude,
        'parking.latitude': this.data.latitude,
        'parking.rentRemark': this.data.rentRemark
      },
      success: function (res) {
        if (res.data.code == 100) {
          console.log("小区车位发布成功");
          that.toParkdetail(res.data.obj.id);
        } else {
          var showCotent = res.data.message;
          wx.showModal({
            title: '提示',
            content: showCotent,
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
              }
            }
          })

        }
      }
    })
  },

  toParkdetail: function (e) {
    var goodsDetailId = e;
    wx.navigateTo({
      url: "/pages/publishedParking/index?id=" + goodsDetailId
    })
  }
})